package com.cheyun.netsalev3.view

import android.util.Log
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.ImageGridAdapter
import com.cheyun.netsalev3.adapter.MyAdapter
import com.cheyun.netsalev3.bean.ClueDetails
import com.cheyun.netsalev3.databinding.ActivityCarBuyDetailsBinding
import com.cheyun.netsalev3.utils.BaseActivity
import com.cheyun.netsalev3.utils.InjectorUtils
import com.cheyun.netsalev3.viewmodel.CarBuyDetailsActivityViewModel
import com.jeremyliao.liveeventbus.LiveEventBus

class CarBuyDetailsActivity : BaseActivity<ActivityCarBuyDetailsBinding>() {
    override fun initContentView(): Int {
        return R.layout.activity_car_buy_details
    }

    var tiCar = false
    lateinit var data: ClueDetails
    override fun initViewModel(): ViewModel? {
        tiCar = intent.getBooleanExtra("tiCar", false)
        data = intent.getSerializableExtra("data") as ClueDetails
        var factory = InjectorUtils.getCarBuyDetailsActivityViewModelFactory(data, tiCar)
        return ViewModelProviders.of(this, factory).get(CarBuyDetailsActivityViewModel::class.java)
    }

    private lateinit var adpater: MyAdapter

    private lateinit var imageAdpater: ImageGridAdapter


    override fun initView() {
        adpater = MyAdapter(R.layout.activity_car_buy_details_item, binding.viewModel)
        imageAdpater = ImageGridAdapter(this);
        binding.recyclerView.adapter = adpater
        binding.imageGrid.adapter = imageAdpater
        if (tiCar) {
            binding.headView.titleText?.text = "提车详情"
        }
        if (!tiCar&&(data.Level=="D" || data.Level == "O")){
            binding.headView.rightBut?.visibility= View.GONE
        }
        LiveEventBus.get()
                .with("cluebuyingmingxi", ClueDetails::class.java)
                .observe(this, Observer {
                    Log.d("mingxi","change")
                    binding.viewModel?.refreshClueDetails(it)
                })
    }

    override fun initData() {
        binding.viewModel?.listdata?.observe(this, Observer { list ->
            adpater.submitList(list)
        })
        binding.viewModel?.imageList?.observe(this, Observer { list ->
            imageAdpater.replaceAll(list)
        })

    }

}
